


var map;
var slider_value = 0;
var START_TIME = new Array("01am", "08am", "02pm", "06pm");
var END_TIME =   new Array("07am", "01pm", "05pm", "12am");
var markersArray = [];


function init(){

    var latlng = new google.maps.LatLng ((north+south)/2,(east+west)/2);

    var myOptions = { zoom: zoom,
                      center: latlng,
                      mapTypeId: google.maps.MapTypeId.TERRAIN
                    };
	
	
    map = new google.maps.Map(document.getElementById("map"), myOptions); 
	
	loadOverlay(dir+"200/",100);
	
	var bikeLayer = new google.maps.BicyclingLayer();
	bikeLayer.setMap(map);
	
	google.maps.event.addListener(map, 'zoom_changed', function() {
    	changeZoom(map.getZoom());
  	});
	
	setMetaData();
}


function changeZoom(z) {
	if(z>13)
		loadOverlay(dir+"100/",90);
	if(z==12 || z==13)
		loadOverlay(dir+"200/",90);
	if(z==11)
		loadOverlay(dir+"200/",90);
	if(z==10)
		loadOverlay(dir+"300/",60);
	if(z==9)
		loadOverlay(dir+"300/",40);
	if(z<9) {
		var xy = map.getCenter();
		window.location = "index.jsp?lat="+xy.lat()+"&lng="+xy.lng()+"&zoom="+z+"&activity="+activity+"&city="+city+"&countryName="+countryName;
		
	}
}

function loadOverlay(dirz, opacity) {

	var imageBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(south,west),
    new google.maps.LatLng(north,east));
	
	if(markersArray.length > 1) {
		for (i=0; i<4;i++) {
			markersArray[i].setMap(null);
		}
	}
	
	for(i=0; i<4;i++) {
		var file = dirz+i+".png"
		markersArray[i] = new ProjectedOverlay(map, file, imageBounds, {percentOpacity:opacity}) ;
		markersArray[i].setMap(null);
	}
	
	markersArray[slider_value].setMap(map);
}


function forward() {
	if(slider_value < 3) {
		slider_value ++;
		update()
	}
}

function rewind() {
	if(slider_value > 0) {
		slider_value --;
		update()
	}
}


function update() {

	for (i=0; i<4;i++) {
		markersArray[i].setMap(null);
	}
	markersArray[slider_value].setMap(map);
	setMetaData();
}



function setMetaData() {
	var st = document.getElementById('start_time');
	var et = document.getElementById('end_time');
	var nu = document.getElementById('n_users');
	var nt = document.getElementById('n_tracks');
	
	
	setText(st, START_TIME[slider_value]); 
	setText(et, END_TIME[slider_value]); 
	setText(nu, n_users[slider_value]); 
	setText(nt, n_tracks[slider_value]); 
}



function setText(tag, text) {
	while (tag.hasChildNodes()) {
        tag.removeChild(tag.firstChild);
    }
    tag.appendChild(document.createTextNode(text));
}


function missingImg(element) {
	element.style.display="none";
	var nu = document.getElementById('n_users');
	var nt = document.getElementById('n_tracks');
	setText(nu, "0"); 
	setText(nt, "0"); 
	
	if(slider_value < 4)
		forward();
}



var timer_is_on = 0;

function play() {
	var p = document.getElementById('play');
	var s = document.getElementById('stop');
	if(timer_is_on == 0) {
		timer_is_on = 1;
		p.className = "hide";
		s.className = "show";
		run();
	}
	else {
		timer_is_on = 0;
		p.className = "show";
		s.className = "hide";
	}
}



function run() {
	if(timer_is_on == 1) {
		forward();
		if(slider_value == 3) slider_value = -1;
		setTimeout("run()",1000);
	}
}


